home *** CD-ROM | disk | FTP | other *** search
/ Languguage OS 2 / Languguage OS II Version 10-94 (Knowledge Media)(1994).ISO / gnu / nihcl-30.lha / nihcl-3.0 / lib / Iterator.c < prev    next >
C/C++ Source or Header  |  1990-05-19  |  4KB  |  194 lines

  1. /* Iterator.c -- Implementation of Collection Iterators
  2.  
  3.     THIS SOFTWARE FITS THE DESCRIPTION IN THE U.S. COPYRIGHT ACT OF A
  4.     "UNITED STATES GOVERNMENT WORK".  IT WAS WRITTEN AS A PART OF THE
  5.     AUTHOR'S OFFICIAL DUTIES AS A GOVERNMENT EMPLOYEE.  THIS MEANS IT
  6.     CANNOT BE COPYRIGHTED.  THIS SOFTWARE IS FREELY AVAILABLE TO THE
  7.     PUBLIC FOR USE WITHOUT A COPYRIGHT NOTICE, AND THERE ARE NO
  8.     RESTRICTIONS ON ITS USE, NOW OR SUBSEQUENTLY.
  9.  
  10. Author:
  11.     K. E. Gorlen
  12.     Bg. 12A, Rm. 2033
  13.     Computer Systems Laboratory
  14.     Division of Computer Research and Technology
  15.     National Institutes of Health
  16.     Bethesda, Maryland 20892
  17.     Phone: (301) 496-1111
  18.     uucp: uunet!nih-csl!kgorlen
  19.     Internet: kgorlen@alw.nih.gov
  20.     December, 1987
  21.  
  22. Function:
  23.  
  24. Instances of class Iterator are used to iterate over (i.e. sequence
  25. through) the objects contained in a Collection.  For example:
  26.  
  27.     OrderedCltn c;
  28.     c.add(*new Point(0,0));
  29.     c.add(*new Point(1,1));
  30. //    ...
  31.     Iterator i(c);
  32.     Object* p;
  33.     while (p = i++) cout << *p;
  34.  
  35. will print all the Point objects in the OrderedCltn c in the same
  36. order in which they were added.
  37.     
  38. Iterators may be used on any derived class of Collection, and several
  39. Iterators may be active on the same Collection at the same time
  40. without interference.
  41.  
  42. Iterator is an NIHCL class and implements the usual Object
  43. functionality with the following restriction: deepCopy() and
  44. storeOn() work only for Iterators bound to classes derived from
  45. SeqCltn.
  46.  
  47. $Log:    Iterator.c,v $
  48.  * Revision 3.0  90/05/20  00:19:55  kgorlen
  49.  * Release for 1st edition.
  50.  * 
  51. */
  52.  
  53. #include "Iterator.h"
  54. #include "Collection.h"
  55. #include "SeqCltn.h"
  56. #include "nihclIO.h"
  57.  
  58. #define    THIS    Iterator
  59. #define    BASE    Object
  60. #define BASE_CLASSES BASE::desc()
  61. #define MEMBER_CLASSES
  62. #define VIRTUAL_BASE_CLASSES Object::desc()
  63.  
  64. DEFINE_CLASS(Iterator,2,"$Header: /afs/alw.nih.gov/unix/sun4_40c/usr/local/src/nihcl-3.0/share/lib/RCS/Iterator.c,v 3.0 90/05/20 00:19:55 kgorlen Rel $",NULL,NULL);
  65.  
  66. Iterator::Iterator(const Collection& c)
  67.     : cltn(&c)
  68. {
  69.     index = 0;
  70.     ptr = NULL;
  71.     num = 0;
  72.     state = nil;
  73.     cltn->doReset(*this);
  74. }
  75.  
  76. void Iterator::reset()
  77. {
  78.     cltn->doReset(*this);
  79. }
  80.  
  81. Iterator::~Iterator()
  82. {
  83.         cltn->doFinish(*this);
  84. }
  85.  
  86. Object* Iterator::operator++()
  87. {
  88.     return ptr = cltn->doNext(*this);
  89. }
  90.  
  91. bool Iterator::operator==(const Iterator& a) const
  92. {
  93.     return cltn->isSame(*a.cltn)
  94.         && index == a.index && num == a.num
  95.         && state->isEqual(*a.state);
  96. }
  97.  
  98. const Class* Iterator::species() const
  99. {
  100.     return Iterator::desc();
  101. }
  102.  
  103. int Iterator::compare(const Object&) const
  104. {
  105.     shouldNotImplement("compare");
  106.     return 0;
  107. }
  108.  
  109. bool Iterator::isEqual(const Object& p) const
  110. {
  111.     return p.isSpecies(classDesc) && *this==castdown(p);
  112. }
  113.  
  114. unsigned Iterator::hash() const
  115. {
  116.     return (const unsigned)cltn ^ index ^ num ^ state->hash();
  117. }
  118.  
  119. void Iterator::deepenShallowCopy()
  120. {
  121. // Can only deepCopy() Iterators over SeqCltns
  122. // -- not Sets, Bags, Dictionaries, etc.
  123.     cltn->assertClass(*SeqCltn::desc());
  124.     cltn = Collection::castdown(cltn->deepCopy());
  125.     state = state->deepCopy();
  126.     if (ptr) ptr = ptr->deepCopy();
  127. }
  128.  
  129. void Iterator::dumpOn(ostream& strm) const
  130. {
  131.     strm << className() << '[';
  132.     strm << cltn->className() << '[' << index << "]#" << num;
  133.     if (state != nil) {
  134.         strm << ' ';
  135.         state->dumpOn(strm);
  136.     }
  137.     strm << "]\n";
  138. }
  139.  
  140. void Iterator::printOn(ostream& strm) const
  141. {
  142.     if (ptr) strm << *ptr;
  143. }
  144.  
  145. Iterator::Iterator(OIOin& strm)
  146.     : BASE(strm)
  147. {
  148.     bool ptrflag;
  149.     strm >> index >> num >> ptrflag;
  150.     cltn = Collection::readFrom(strm);
  151.     cltn->assertClass(*SeqCltn::desc());
  152.     state = Object::readFrom(strm);
  153.     if (ptrflag) ptr = Object::readFrom(strm);
  154. }
  155.  
  156. void Iterator::storer(OIOout& strm) const
  157. {
  158. // Can only storeOn() Iterators over SeqCltns
  159. // -- not Sets, Bags, Dictionaries, etc.
  160.     cltn->assertClass(*SeqCltn::desc());
  161.     BASE::storer(strm);
  162.     strm << index << num << (ptr ? YES : NO);
  163.     cltn->storeOn(strm);
  164.     state->storeOn(strm);
  165.     if (ptr) ptr->storeOn(strm);
  166. }
  167.  
  168. Iterator::Iterator(OIOifd& fd)
  169.     : BASE(fd)
  170. {
  171.     bool ptrflag;
  172.     fd >> index;
  173.     fd >> num;
  174.     fd >> ptrflag;
  175.     cltn = Collection::readFrom(fd);
  176.     cltn->assertClass(*SeqCltn::desc());
  177.     state = Object::readFrom(fd);
  178.     if (ptrflag) ptr = Object::readFrom(fd);
  179. }
  180.  
  181. void Iterator::storer(OIOofd& fd) const
  182. {
  183. // Can only storeOn() Iterators over SeqCltns
  184. // -- not Sets, Bags, Dictionaries, etc.
  185.     cltn->assertClass(*SeqCltn::desc());
  186.     BASE::storer(fd);
  187.     fd << index;
  188.     fd << num;
  189.     fd << (ptr ? YES : NO);
  190.     cltn->storeOn(fd);
  191.     state->storeOn(fd);
  192.     if (ptr) ptr->storeOn(fd);
  193. }
  194.